package com.thinkSoft.Location.Lib;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.provider.Settings.Secure;

import com.thinkSoft.Location.Services.Background;
 
public class DataFunc {
 
    private WebData jsonParser;
    private static String loginURL = "http://zappsecurity.com/test/backend/index.php";
    private static String registerURL = "http://zappsecurity.com/test/backend/index.php";
    private static String motd_tag = "motd";
    private static String login_tag = "login";
    private static String register_tag = "register";
    private static String device_tag = "device";
    private static String location_tag = "location";
    
    public DataFunc(){
        jsonParser = new WebData();
    }
 
    public JSONObject loginUser(String email, String password){
    	
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", login_tag));
        params.add(new BasicNameValuePair("email", email));
        params.add(new BasicNameValuePair("password", password));
        JSONObject json = jsonParser.getJSONFromUrl(loginURL, params);
        return json;
    }
 
    public JSONObject registerUser(String name, String email, String password){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", register_tag));
        params.add(new BasicNameValuePair("name", name));
        params.add(new BasicNameValuePair("email", email));
        params.add(new BasicNameValuePair("password", password));
        JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
        
        return json;
    }
    
    public JSONObject getMOTD(){
    	
    	List<NameValuePair> params = new ArrayList<NameValuePair>();
    	params.add(new BasicNameValuePair("tag", motd_tag));
    	// getting JSON Object
        JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
        // return json
        return json;
    }
 
    public boolean isUserLoggedIn(Context context){
        LocalDatabase db = new LocalDatabase(context);
        int count = db.getRowCount();
        if(count > 0){
            return true;
        }
        return false;
    }
 
    public boolean logoutUser(Context context){
        LocalDatabase db = new LocalDatabase(context);
        db.resetTables();
        context.stopService(new Intent(Background.class.getName()));
        return true;
    }
    
    public JSONObject sendDeviceInfo(Context context){
    	LocalDatabase ld = new LocalDatabase(context);
    	HashMap<String, String> user = ld.getUserDetails();
    	String uid = (String)user.get("uid");
    	
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", device_tag));
        params.add(new BasicNameValuePair("unique_id", uid));
        params.add(new BasicNameValuePair("version", Build.VERSION.RELEASE));
        params.add(new BasicNameValuePair("serial", Secure.getString(context.getContentResolver(), Secure.ANDROID_ID)));
        params.add(new BasicNameValuePair("model", Build.MODEL));
 
        JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
        return json;
    	
    }
    public JSONObject sendDeviceLocation(Context context, String lon, String lat, String alt){
    	LocalDatabase ld = new LocalDatabase(context);
    	HashMap<String, String> user = ld.getUserDetails();
    	String uid = (String)user.get("uid");
    	
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", location_tag));
        params.add(new BasicNameValuePair("unique_id", uid));
        params.add(new BasicNameValuePair("serial", Secure.getString(context.getContentResolver(), Secure.ANDROID_ID)));
        params.add(new BasicNameValuePair("long", lon));
        params.add(new BasicNameValuePair("lat", lat));
        params.add(new BasicNameValuePair("alt", alt));
 
        JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
        return json;
    	
    }
 
}